<?php
	/*****************************************************************
		GP Clinic - a web based Medical directory system
		Copyright (C) 2014 Kaizensynergy - Gemba360

		Author: Bless Bauson
		Key: bHkTy89wZ0DHyUiim0041rTxyBbP671aQwg327UtjfcrO
	*****************************************************************/
	
	if($_SERVER['REQUEST_METHOD'] == "GET" && isset($_GET['row']) && !empty($_GET['row'])){
		$patient_id = $_GET['row'];

		//fetch patient details
		$params = array('filter' => 'patient.id = '.$patient_id);
		$patient = new Patient($params);
		$patient->SQL_AddJoin("LEFT JOIN", "atsi", "atsi.id = patient.atsi", "atsi.label AS atsi_label");
		$patient->SQL_AddJoin("LEFT JOIN", "health_fund", "health_fund.id = patient.id=health_fund_name", "health_fund.label AS health_fund_label");
		$patient->SQL_AddJoin("LEFT JOIN", "health_fund_type", "health_fund_type.id = patient.health_fund_type", "health_fund_type.label AS health_fund_type_label");
		$patient_row = $patient->SQL_Select();

		//fetch patient group details
		$gparams = array('filter' => 'patient_group.patient_id = '.$patient_id);
		$groups  =  new PatientGroup($gparams);
		$groups->SQL_AddJoin("INNER JOIN", "groups", "groups.id = patient_group.group_id", "groups.name AS group_name");
		$results = $groups->SQL_SelectAll();

		if(count($results)){
			foreach($results as $gkey => $gval){
				$groups_row[] = $gval['group_name'];
			}
		}
		
		//fetch patient users details
		$uparams = array('filter' => 'user_patient.patient_id = '.$patient_id);
		$users   = new UserPatient($uparams);
		$users->SQL_AddJoin("INNER JOIN", "groups", "groups.id = user_patient.user_group_id", "groups.name AS group_name");
		$users->SQL_AddJoin("INNER JOIN", "user", "user.id = user_patient.user_id", "user.user_type_id AS user_type_id");
		$users->SQL_AddJoin("INNER JOIN", "user_type AS ut", "user.user_type_id = ut.id", "ut.label AS user_type_label");
		$users->SQL_AddJoin("LEFT JOIN", "user_practicioner", "user_practicioner.user_id = user_patient.user_id", "user_practicioner.name AS user_name1");
		$users->SQL_AddJoin("LEFT JOIN", "user_non_practicioner", "user_non_practicioner.user_id = user_patient.user_id", "user_non_practicioner.name AS user_name2");
		$uresults = $users->SQL_SelectAll();

		if(count($uresults)){
			foreach($uresults as $ukey => $uval){
				$users_row[$uval['group_name']][] = $uval['user_name1'].$uval['user_name2']." (".$uval['user_type_label'].")";
			}
		}
	}
?>